<?php
Yii::import('zii.widgets.jui.CJuiInputWidget');

class DatePicker extends CJuiInputWidget {

	public $flat = false;

	public function run() {
		list($name, $id) = $this->resolveNameID();
		if(isset($this->htmlOptions['id'])) {
			$id = $this->htmlOptions['id'];
		} else {
			$this->htmlOptions['id'] = $id;
		}
		if(isset($this->htmlOptions['name'])) {
			$name = $this->htmlOptions['name'];
		}
		if($this->flat === false) {
			if($this->hasModel()) {
				echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
			} else {
				echo CHtml::textField($name, $this->value, $this->htmlOptions);
			}
		} else {
			if($this->hasModel()) {
				echo CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions);
				$attribute                    = $this->attribute;
				$this->options['defaultDate'] = $this->model->$attribute;
			} else {
				echo CHtml::hiddenField($name, $this->value, $this->htmlOptions);
				$this->options['defaultDate'] = $this->value;
			}
			$this->options['altField'] = '#' . $id;
			$id                        = $this->htmlOptions['id'] = $id . '_container';
			$this->htmlOptions['name'] = $name . '_container';
			echo CHtml::tag('div', $this->htmlOptions, '');
		}
		$options = CJavaScript::encode($this->options);
		$js      = "jQuery('#{$id}').datepicker({$options});";
		$cs      = Yii::app()->getClientScript();
		if(isset($this->defaultOptions)) {
			$cs->registerScript(__CLASS__, $this->defaultOptions !== null ? 'jQuery.datepicker.setDefaults(' . CJavaScript::encode($this->defaultOptions) . ');' : '');
		}
		$cs->registerScript(__CLASS__ . '#' . $id, $js);
	}
}